package com.ztc.ZtcClassesPrice.web;
import com.aqgj.User.bean.User;
import com.ztc.ZtcClassesForSale.bean.ZtcClassesForSale;
import com.ztc.ZtcClassesForSale.dao.ZtcClassesForSaleDao;
import com.ztc.ZtcClassesForSale.service.ZtcClassesForSaleManager;
import com.ztc.ZtcClassesPrice.dao.ZtcClassesPriceDao;
import com.ztc.ZtcClassesSite.bean.ZtcClassesSite;
import com.ztc.ZtcClassesSite.dao.ZtcClassesSiteDao;
import com.ztc.ZtcTransport.bean.ZtcTransport;
import com.ztc.ZtcTransport.service.ZtcTransportManager;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ztc.ZtcClassesPrice.bean.ZtcClassesPrice;
import com.ztc.ZtcClassesPrice.service.ZtcClassesPriceManager;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
@Controller
@RequestMapping("ztc_classes_price_action")
public class ZtcClassesPriceAction{
    @Autowired
    private ZtcClassesPriceManager  ztcClassesPriceManager;
	@Autowired
	private ZtcClassesForSaleManager ztcClassesForSaleManager;
	@Autowired
	private ZtcClassesForSaleDao ztcClassesForSaleDao;
	@Autowired
	private ZtcClassesPriceDao ztcClassesPriceDao;
	@Autowired
	private ZtcClassesSiteDao ztcClassesSiteDao;
	@Autowired
	private ZtcTransportManager ztcTransportManager;
    private static final Logger log = LogManager.getLogger(ZtcClassesPrice.class);
	@RequestMapping("init")
	@ResponseBody
	public HashMap<String, Object> getAll() {
		List<ZtcClassesPrice> lists = ztcClassesPriceManager.getAll();
		HashMap<String, Object> wapper = new HashMap<>();
		wapper.put("success", Boolean.valueOf(true));
		wapper.put("data", lists);
		return wapper;
	}

	/**
	 * 根据起始站点名称，结束站点名称模糊查找价格信息
	 * @param startClassesSiteName 起始站点名称
	 * @param endClassesSiteName 结束站点名称
	 * @return 价格信息列表
	 */
	@RequestMapping("searchInfoLikes")
	@ResponseBody
	public HashMap<String, Object> searchInfoLikes(String startClassesSiteName,String endClassesSiteName) {
		List<ZtcClassesPrice> lists = ztcClassesPriceManager.searchInfoLikes(startClassesSiteName,endClassesSiteName);
		HashMap<String, Object> wapper = new HashMap<>();
		wapper.put("success", Boolean.valueOf(true));
		wapper.put("data", lists);
		return wapper;
	}

	/**
	 * 修改价格
	 * @param id 价格id
	 * @param distance 距离
	 * @param price 价格
	 * @return 操作信息：0.修改失败1.修改成功
	 */
	@RequestMapping("update")
	@ResponseBody
	public HashMap<String, Object> update(String id,String distance,String price) {
		ZtcClassesPrice classesPrice=new ZtcClassesPrice();
		classesPrice.setDistance(distance);
		classesPrice.setPrice(price);
		classesPrice.setId(id);
		String result=ztcClassesPriceManager.update(classesPrice);
		HashMap<String, Object> wapper = new HashMap<>();
		if(result!=null){
			wapper.put("success", Boolean.valueOf(true));
			wapper.put("data", "修改成功");
			log.info("修改成功");
		}else{
			wapper.put("success", Boolean.valueOf(true));
			wapper.put("data", "修改成功");
			log.info("修改成功");
		}
		return wapper;
	}

	/**
	 * 停售在售班次价格
	 * @param classesId 班次模板id
	 * @param ids 班次模板价格id集合
	 * @param session
	 * @return 操作结果信息
	 */
	@RequestMapping("stopSell")
	@ResponseBody
	public HashMap<String,Object> stopSell(String classesId,String ids,HttpSession session){
		HashMap<String, Object> wapper = new HashMap<>();
		User user = (User) session.getAttribute("userInfo");
		Date now=new Date();
		if (user == null || "".equals(user)) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "用户未登录");
			log.info("用户未登录");
			return wapper;
		}
		ZtcTransport transport = ztcTransportManager.get(user.getTransportId());
		int transportType = transport.getType();
		if (transportType == 0 || transportType == 1 || transportType == 2) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "非运营公司或车方不可操作班次");
			log.info("非运营公司或车方不可操作班次");
			return wapper;
		}
		if (transport.getStatus() == 4) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "该用户公司已被冻结，不可停售班次");
			log.info("该用户公司已被冻结，不可停售班次");
			return wapper;
		}
		List<String> startAndEndList=new ArrayList<>();
		ids = ids.substring(1, ids.length() - 1);
		ids = ids.replace("\"", "");
		String[] idList=ids.split(",");
		for(int i=0;i<idList.length;i++){
			ZtcClassesPrice price=ztcClassesPriceDao.get(idList[i]);
			ZtcClassesSite startSite=ztcClassesSiteDao.get(price.getFkStartClassesSiteId());
			ZtcClassesSite endSite=ztcClassesSiteDao.get(price.getFkEndClassesSiteId());
			startAndEndList.add(startSite.getSequence()+","+endSite.getSequence());
		}
		ZtcClassesForSale classesForSale=new ZtcClassesForSale();
		classesForSale.setFkZtcClassesId(classesId);
		classesForSale.setTicketStatus(0);
		List<ZtcClassesForSale> classesForSaleList=ztcClassesForSaleManager.searchInfoEquals(classesForSale);
		String result=ztcClassesPriceManager.stopSell(startAndEndList,classesForSaleList,user,now);
		if("0".equals(result)){
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "停售失败");
			log.info("停售失败");
			return wapper;
		}
		wapper.put("success", Boolean.valueOf(true));
		wapper.put("data", "停售成功");
		log.info("停售成功");
		return wapper;
	}

	/**
	 * 起售在售班次价格
	 * @param classesId 班次模板id
	 * @param ids 班次模板价格id集合
	 * @param session
	 * @return 操作结果信息
	 */
	@RequestMapping("restartToSell")
	@ResponseBody
	public HashMap<String,Object> restartToSell(String classesId,String ids,HttpSession session){
		HashMap<String, Object> wapper = new HashMap<>();
		Date now=new Date();
		User user = (User) session.getAttribute("userInfo");
		if (user == null || "".equals(user)) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "用户未登录");
			log.info("用户未登录");
			return wapper;
		}
		ZtcTransport transport = ztcTransportManager.get(user.getTransportId());
		int transportType = transport.getType();
		if (transportType == 0 || transportType == 1 || transportType == 2) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "非运营公司或车方不可操作班次");
			log.info("非运营公司或车方不可操作班次");
			return wapper;
		}
		if (transport.getStatus() == 4) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "该用户公司已被冻结，不可停售班次");
			log.info("该用户公司已被冻结，不可停售班次");
			return wapper;
		}
		List<String> startAndEndList=new ArrayList<>();
		ids = ids.substring(1, ids.length() - 1);
		ids = ids.replace("\"", "");
		String[] idList=ids.split(",");
		for(int i=0;i<idList.length;i++){
			ZtcClassesPrice price=ztcClassesPriceDao.get(idList[i]);
			ZtcClassesSite startSite=ztcClassesSiteDao.get(price.getFkStartClassesSiteId());
			ZtcClassesSite endSite=ztcClassesSiteDao.get(price.getFkEndClassesSiteId());
			startAndEndList.add(startSite.getSequence()+","+endSite.getSequence());
		}
		List<ZtcClassesForSale> classesForSaleList=ztcClassesForSaleDao.searchInfoWithClassesId(classesId);
		String result=ztcClassesPriceManager.restartToSell(startAndEndList,classesForSaleList,user,now);
		if("0".equals(result)){
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "起售失败");
			log.info("起售失败");
			return wapper;
		}
		wapper.put("success", Boolean.valueOf(true));
		wapper.put("data", "起售成功");
		log.info("起售成功");
		return wapper;
	}
}